unit uFuncao;

interface

uses DB, SysUtils;

function ValidaData (s: String): boolean;
function IntegerValido(s: String): boolean;
function FormataValor(v: Currency): String;
function ValidaEmail(aStr: string): Boolean;
Function validacnpj(num: string): boolean;
Function validacpf(cpf:string):boolean;

implementation

function ValidaData(s: String): boolean;
begin
   Result := True;
   try
      StrToDate(s);
   except
      Result := False;
   end;
end;

function IntegerValido(s: String): boolean;
begin
  Result := True;
  try
    StrToInt(s);
  except
    Result := False;
  end;
end;

function FormataValor(v: Currency): String;
begin
   Result := FormatFloat('#,##0.00', v);
end;

function ValidaEmail(aStr: string): Boolean;
begin
  aStr := Trim(UpperCase(aStr));
  if Pos('@', aStr) > 1 then
  begin
    Delete(aStr, 1, pos('@', aStr));
    Result := (Length(aStr) > 0) and (Pos('.', aStr) > 2);
  end
  else
    Result := False;
end;



function validacnpj(num: string): boolean;
var
  n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12: integer;
  d1,d2: integer;
  digitado, calculado: string;
  i: integer;
  want: char;
  wvalid: boolean;
begin
  Want := num[1];
  Delete(num,ansipos('.',num),1);
  Delete(num,ansipos('.',num),1);
  Delete(num,ansipos('-',num),1);

  for i:=1 to length(num) do
   begin
     if num[i] <> Want then
       begin
          Wvalid:=true;
          break
       end;
   end;

   if not Wvalid then
   begin
      result:=false;
      exit;
   end;

    n1:=StrToInt(num[1]);
    n2:=StrToInt(num[2]);
    n3:=StrToInt(num[3]);
    n4:=StrToInt(num[4]);
    n5:=StrToInt(num[5]);
    n6:=StrToInt(num[6]);
    n7:=StrToInt(num[7]);
    n8:=StrToInt(num[8]);
    n9:=StrToInt(num[9]);
    n10:=StrToInt(num[10]);
    n11:=StrToInt(num[11]);
    n12:=StrToInt(num[12]);

    d1:=n12*2+n11*3+n10*4+n9*5+n8*6+n7*7+n6*8+n5*9+n4*2+n3*3+n2*4+n1*5;
    d1:=11-(d1 mod 11);
   if d1>=10 then d1:=0;
     d2:=d1*2+n12*3+n11*4+n10*5+n9*6+n8*7+n7*8+n6*9+n5*2+n4*3+n3*4+n2*5+n1*6;
     d2:=11-(d2 mod 11);
   if d2>=10 then d2:=0;
     calculado:=inttostr(d1)+inttostr(d2);
     digitado:=num[13]+num[14];
   if calculado=digitado then
    validacnpj:=true
   else
    validacnpj:=false;
end;


Function validacpf(cpf:string):boolean;
var
  i:integer;
  Want:char;
  Wvalid:boolean;
  Wdigit1,Wdigit2:integer;
begin
  Wdigit1:=0;
  Wdigit2:=0;
  Want:=cpf[1];
  Delete(cpf,ansipos('.',cpf),1);
  Delete(cpf,ansipos('.',cpf),1);
  Delete(cpf,ansipos('-',cpf),1);

  for i:=1 to length(cpf) do
  begin
   if cpf[i] <> Want then
     begin
        Wvalid:=true;
        break
     end;
  end;

  if not Wvalid then
  begin
    result:=false;
    exit;
  end;

  for i:=1 to 9 do
  begin
    wdigit1:=Wdigit1+(strtoint(cpf[10-i])*(I+1));
  end;

  Wdigit1:= ((11 - (Wdigit1 mod 11))mod 11) mod 10;

  if IntToStr(Wdigit1) <> cpf[10] then
  begin
   result:=false;
   exit;
  end;

  for i:=1 to 10 do
  begin
    wdigit2:=Wdigit2+(strtoint(cpf[11-i])*(I+1));
  end;
    Wdigit2:= ((11 - (Wdigit2 mod 11))mod 11) mod 10;

  if IntToStr(Wdigit2) <> cpf[11] then
  begin
     result:=false;
     exit;
  end;

  result:=true;
end;

end.
